﻿var WIDGETS = WIDGETS || {};

WIDGETS["menu-hint-result"] = function ($element) {

    var proto = {
        $element: $($element),
        $btnTime: $(),

        init: function () {
            this.discoverChildren();
            this.bindEvents();
            this.storeSelf();
        },

        discoverChildren: function () {
            this.$actions = this.$element.find(".actions");
            this.$hintTitle = this.$element.find(".title");
            this.$hintDetails = this.$element.find(".hint-candidate");
        },

        bindEvents: function () {
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchHintResult, this.onHintsResult.bind(this));
        },

        storeSelf: function () {
            this.$element.data("menu-hint-result", this);
        },

        onHintsResult: function (ev, result) {
            if (!result) {
                this.$hintTitle.text("No hints ^-^'");
                this.$hintDetails.text("");
                this.$actions.hide();
                return;
            }

            this.$actions.show();

            if (result.strategy instanceof Strategies.NakedSingle) {
                this.$hintTitle.text("Naked Single");
                this.$hintDetails.text(result.candidate);
            }
            else if (result.strategy instanceof Strategies.HiddenSingle) {
                this.$hintTitle.text("Hidden Single");
                this.$hintDetails.text(result.candidate);
            }
            else if (result.strategy instanceof Strategies.PointingPair) {
                this.$hintTitle.text("Pointing Pair");
                this.$hintDetails.text(result.candidate);
            }
            else if (result.strategy instanceof Strategies.PointingInside) {
                this.$hintTitle.text("Box / Line Reduction");
                this.$hintDetails.text(result.candidate);
            }
            else if (result.strategy instanceof Strategies.NakedPair) {
                var pairText = "Pair";
                if (result.candidates.length == 3) {
                    pairText = "Triple";
                }
                else if (result.candidates.length == 4) {
                    pairText = "Quadruple";
                }

                this.$hintTitle.text("Naked " + pairText);
                this.$hintDetails.text(result.candidates.join());
            }
            else if (result.strategy instanceof Strategies.Burma) {
                var burmaText = "X-Wing";
                if (result.burmaSize == 3) {
                    burmaText = "Swordfish";
                }
                else if (result.burmaSize == 4) {
                    burmaText = "Jellyfish";
                }

                this.$hintTitle.text(burmaText);
                this.$hintDetails.text(result.candidate);
            }

            else if (result.strategy instanceof Strategies.SimpleColoring) {

                var rules = [];
                if (result.cellsConflict.length > 0)
                    rules.push("Chain Conflict");
                if (result.cellsSeeTwo.length > 0)
                    rules.push("See two colors");

                var details = " (" + rules.join("&") + ")";
                this.$hintTitle.text("Simple Coloring");
                this.$hintDetails.text(result.candidate + details);
            }
        }
    }

    proto.init();

    return proto;
}
